package com.xywh.composeLibrary.ext

import android.content.Context
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.xywh.composeLibrary.R
import com.xywh.composeLibrary.statex.StateComposeConfig
import com.xywh.composeLibrary.view.XxImageVector
import com.xywh.composeLibrary.view.loading.ThreeBallLoading
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


fun stateComposeConfig(context: Context) {

    StateComposeConfig.apply {
        // 将tag传递出来,对于compose而言,我们有时需要对界面进行重绘,所以携带了传递的数据,以便做自定义处理
        errorComponent { _, onClick ->
            // Compose中插入原生

            AndroidView(
                {
                    View.inflate(it, R.layout.item_state_error, null).apply {
                        findViewById<TextView>(R.id.tvErrorHint).text =
                            tag?.toString() ?: context.getString(R.string.tv_status_error_hint)

                        findViewById<LinearLayout>(R.id.errorId).setOnClickListener {


                            Log.e("stateComposeConfig:", "errorComponent--onclick")
                            onClick.invoke()
                        }

                    }
                },
                Modifier
                    .fillMaxSize()
            )
        }
        loadingComponent {
//                CircularProgressIndicator(
//                    modifier = Modifier
//                        .size(100.dp)
//                        .align(
//                            Alignment.Center
//                        )
//                )

            ThreeBallLoading(
                modifier = Modifier
                    .align(Alignment.Center)
                    .width(200.dp)
                    .height(200.dp)
            )
        }
        emptyComponent() { _, onClick ->

            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .align(
                        Alignment.Center
                    )
                    .clickable {
                        Log.e("stateComposeConfig:", "emptyComponent--onclick")

                        onClick.invoke()
                    }
            ) {
                Image(
                    alignment = Alignment.Center,
                    modifier = Modifier
                        .padding(bottom = 10.dp)
                        .height(150.dp),
                    contentScale = ContentScale.FillHeight,
                    painter = painterResource(id = R.drawable.ic_state_empty),
                    contentDescription = ""
                )
            }

        }
    }
}


@Composable
fun ListFloatingActionButton(
    scope: CoroutineScope,
    listState: LazyListState,
    scrollToTop: () -> Unit = {}
) {
    val showFloatButton by remember {
        derivedStateOf {
            listState.firstVisibleItemIndex > 0
        }
    }

    if (showFloatButton)
        FloatingActionButton(
            containerColor = MaterialTheme.colorScheme.primary,
            onClick = {
                scope.launch {
                    listState.animateScrollToItem(index = 0)
                    scrollToTop.invoke()
                }
            }
        ) {
            XxImageVector(
                imageVectorId = R.drawable.ic_arrow_upward_white_24dp,
                contentDescription = "",
                size = 30.dp
            )
        }
}
